<?php
$strWeb = $_SERVER[argv][1];
//$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/".$strWeb."/secure";
$_SERVER["DOCUMENT_ROOT"] = "/var/www/lottou";

$strError = $strWeb."  (".date("Y-m-d H:i:s").")\n";
$strError .= "===========================================\n";

$_isDBExist = "no";
$_DBSite = "Master";
if(!file_exists($_SERVER["DOCUMENT_ROOT"]."/config.inc.php"))exit($strError." no config.inc.php");
//include("/home/share/www/SourceSite-utf8/secure/config.inc.php");
include($_SERVER["DOCUMENT_ROOT"]."/config.inc.php");
include(__Root_Path."/config.inc.php");
print __DBConnect_File;
include(__DBConnect_File);

if($_strLogErr != ""){
	@mysql_close($$_Conn);
	exit($strError.$strLogError.$_strLogErr);
}
//if(!preg_match("/utf8/i", $_blDBName))exit($strError.$_blDBName." no UTF8 DB");

//$preBT = $_data["preBT"];
//$newBT = $_data["newBT"];
//if(empty($preBT))$preBT = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22";
//if(empty($newBT))$newBT = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22";
//if(empty($preBT))$preBT = "1,1,1,1,1,1,1,1,1,1,1,1,1";
//if(empty($newBT))$newBT = "31,32,33,34,35,36,37,38,39,40,41,42,43";
if(empty($preBT))$preBT = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35";
if(empty($newBT))$newBT = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35";
$_DBSite = "Master";

$arrPreLID = array(1);
$arrLottoID = array(5);
//$arrPreLID = array(2, 3);
//$arrLottoID = array(2, 3);
//$arrPreLID = array(1);
//$arrLottoID = array(4);

//$arrGLine = array( 93 => 82 );

$arrGLine = array(
				1 => 86.5, 2 => 96.5, 3 => 96.5, 4 => 96.5, 5 => 86.5, 6 => 86.5, 
				7 => 86.5, 8 => 86.5, 9 => 96.5, 10 => 96.5, 
				11 => 96.5, 12 => 96.5, 13 => 96.5,
				14 => 94.5, 15 => 84.5, 16 => 84.5,
				17 => 84.5, 18 => 84.5, 19 => 84.5,
				20 => 84.5, 21 => 84.5, 22 => 84.5,
				23 => 84.5, 24 => 85, 25 => 85,
				26 => 85, 27 => 85, 28 => 85,
				29 => 85, 30 => 85, 31 => 85, 32 => 85, 33 => 85, 34 => 85, 35 => 85
			);

$arrLimt = array(
				1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 
				7 => 0, 8 => 0, 9 => 0, 10 => 0, 
				11 => 0, 12 => 0, 13 => 0,
				14 => 0, 15 => 0, 16 => 0,
				17 => 0, 18 => 0, 19 => 0,
				20 => 0, 21 => 0, 22 => 0,
				23 => 0, 24 => 0, 25 => 0,
				26 => 0, 27 => 0, 28 => 0,
				29 => 0, 30 => 0, 31 => 0, 32 => 0, 33 => 0, 34 => 0, 35 => 0
			);
/*$arrGLine = array(
				1 => 100, 2 => 100, 3 => 100, 4 => 100, 5 => 100, 6=> 100, 
				7 => 100, 8 => 100, 9 => 100, 10 => 100, 
				11 => 100, 12 => 100, 13 => 100, 
				14 => 100, 15 => 100, 16 => 100, 17 => 100, 
				18 => 100, 19 => 100, 20 => 100, 21 => 100, 22 => 100
			);

$arrLimt = array(
				1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 
				11 => 0, 12 => 0, 13 => 0, 14 => 0, 15 => 0, 16 => 0, 17 => 0, 18 => 0, 19 => 0, 20 => 0, 
				21 => 0, 22 => 0
			);
*/
$arrPreBT = explode(",", $preBT);
$arrNewBT = explode(",", $newBT);
//退水
foreach($arrLottoID as $intKK => $intLottoID){
	$intPreLID = $arrPreLID[$intKK];
	foreach($arrPreBT as $intK => $strPreBT){
		$strNewBT = $arrNewBT[$intK];
		//$strPreBT += 50;

		$strSQL= "select * from `bet_Commission` where Bet_Type = ".$strPreBT." and LottoID = ".$intPreLID;
		if(!$RS = @PMA_mysql_query($strSQL, $_ConnMaster)){
			$strError .= "Find Data Error!<br>\n";
			$strError .= "SQL: ".$strSQL."<br>\n";
			$strError .= "Error: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."<br>\n";
			exit($strError);
		}

		if(@PMA_mysql_num_rows($RS) == 0){
			$strError .= "Can not Find Data!!<br>\n";
			$strError .= "SQL: ".$strSQL."<br>\n";
			$strError .= "Error: , \nLine: ".__LINE__.", \nPage: ".__FILE__."<br>\n";
			exit($strError);
		}

		while($Rows = PMA_mysql_fetch_object($RS)){
			$strSQL = "select ID from `bet_Commission` where Bet_Type = ".$strNewBT." and LottoID = ".$intLottoID." and MemID = ".$Rows -> MemID." and Gamble_Type  = ".$Rows -> Gamble_Type;
			if(!$subRS = @PMA_mysql_query($strSQL, $_ConnMaster)){
				$strError .= "Find Data Error!<br>\n";
				$strError .= "SQL: ".$strSQL."<br>\n";
				$strError .= "Error: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."<br>\n";
				exit($strError);
			}
			if(@PMA_mysql_num_rows($subRS) > 0)continue;
			/********************************************************/
			//$Rows -> Commission = '97';
			/********************************************************/
			if(sizeof($arrGLine) > 0){
				$strSQL = "insert into `bet_Commission`(Bet_Type, LottoID, MemID, Gamble_Type, Commission) values(".$strNewBT.", ".$intLottoID.", ".$Rows -> MemID.", ".$Rows -> Gamble_Type.", '".$arrGLine[(int)$strNewBT]."')";
			}else{
				$strSQL = "insert into `bet_Commission`(Bet_Type, LottoID, MemID, Gamble_Type, Commission) values(".$strNewBT.", ".$intLottoID.", ".$Rows -> MemID.", ".$Rows -> Gamble_Type.", '".$Rows -> Commission."')";
			}
			PMA_mysql_query($strSQL, $_ConnMaster);
		}
		$strError .= "BT:".$strPreBT." -> ".$strNewBT." (".$intLottoID.") Commission OK! <br />\n";
	} //foreach($arrPreBT as $intK => $strPreBT)//
} //foreach($arrLottoID as $intLottoID){


//限額
foreach($arrLottoID as $intKK => $intLottoID){
	$intPreLID = $arrPreLID[$intKK];
	foreach($arrPreBT as $intK => $strPreBT){
		$strNewBT = $arrNewBT[$intK];
		//$strPreBT += 50;

		$strSQL= "select * from `bet_MemberLimit` where Bet_Type = ".$strPreBT." and LottoID = ".$intPreLID;
		if(!$RS = @PMA_mysql_query($strSQL, $_ConnMaster)){
			$strError .= "Find Data Error!<br>\n";
			$strError .= "SQL: ".$strSQL."<br>\n";
			$strError .= "Error: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."<br>\n";
			exit($strError);
		}

		if(@PMA_mysql_num_rows($RS) == 0){
			$strError .= "Can not Find Data!!<br>\n";
			$strError .= "SQL: ".$strSQL."<br>\n";
			$strError .= "Error: , \nLine: ".__LINE__.", \nPage: ".__FILE__."<br>\n";
			exit($strError);
		}

		while($Rows = PMA_mysql_fetch_object($RS)){
			$strSQL = "select ID from `bet_MemberLimit` where Bet_Type = ".$strNewBT." and LottoID = ".$intLottoID." and MemID = ".$Rows -> MemID." and LimitType  = ".$Rows -> LimitType;
			if(!$subRS = @PMA_mysql_query($strSQL, $_ConnMaster)){
				$strError .= "Find Data Error!<br>\n";
				$strError .= "SQL: ".$strSQL."<br>\n";
				$strError .= "Error: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."<br>\n";
				exit($strError);
			}
			if(@PMA_mysql_num_rows($subRS) > 0)continue;
			if(sizeof($arrLimt) > 0){
				$intLimit = $arrLimt[(int)$strNewBT];
				if($Rows -> LimitType == 1)$intLimit = $intLimit / 2;
				$strSQL = "insert into `bet_MemberLimit`(Bet_Type, LottoID, MemID, LimitType, LimitValue) values(".$strNewBT.", ".$intLottoID.", ".$Rows -> MemID.", ".$Rows -> LimitType.", '".$intLimit."')";
			}else{
				$strSQL = "insert into `bet_MemberLimit`(Bet_Type, LottoID, MemID, LimitType, LimitValue) values(".$strNewBT.", ".$intLottoID.", ".$Rows -> MemID.", ".$Rows -> LimitType.", '".$Rows -> LimitValue."')";
			}
			PMA_mysql_query($strSQL, $_ConnMaster);
			//print $strSQL;
		}
		$strError .= "BT:".$strPreBT." -> ".$strNewBT." (".$intLottoID.") Limit OK! <br />\n";
	} //foreach($arrPreBT as $intK => $strPreBT)//
} //foreach($arrLottoID as $intLottoID){

print $strError."\nEnd\n";
?>